package com.glory.good_dark.activity;

import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.good_dark.R;
import com.glory.good_dark.application.App;
import com.glory.good_dark.entity.UnlockEntity;
import com.glory.good_dark.entity.response.BaseEntity;
import com.glory.good_dark.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * FileName: LockmachineActivity
 * Author: XJH
 * Date: 2020/3/26 11:15
 * Description: 锁机
 * History:
 */
public class LockmachineActivity extends BaseProjectActivity{

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.EQPID_Txt)
    TextView EQPIDTxt;
    @BindView(R.id.tvLockType)
    TextView tvLockType;
    @BindView(R.id.ivLockTypeListArrow)
    ImageView ivLockTypeListArrow;
    @BindView(R.id.flLockTypeList)
    FrameLayout flLockTypeList;

    private List<String> mLockTypeList;
    private FreeUI_GeneralPop lockTypePop;



    @Override
    protected int getLayoutResId() {
        return R.layout.activity_lockmachine;
    }

    @Override
    protected void initView() {
        EQPIDTxt.setText(App.EQPID);
        mLockTypeList = new ArrayList<>();
    }

    @Override
    protected void initData() {
//        String ss = "{\n" +
//                "    \"data\":{\n" +
//                "        \"HEADER\":{\n" +
//                "            \"MACHINENAME\":\"PDA-MT9001\",\n" +
//                "            \"MESSAGENAME\":\"PDALockInfo\",\n" +
//                "            \"TRANSACTIONID\":\"c5e5e318-389d-4c39-b914-e4c10ba519d4\",\n" +
//                "            \"EVENTTIME\":\"2019-03-16 16:28:59.585\"\n" +
//                "        },\n" +
//                "        \"BODY\":{\n" +
//                "            \"EQP_ID\":\"LC128\",\n" +
//                "            \"LOCK_TYPE_LIST\":[\n" +
//                "                {\n" +
//                "                    \"LOCK_TYPE\":\"手动锁机\",\n" +
//                "                    \"PROCESS\":\"TS00001\",\n" +
//                "                    \"LOCK_ID\":122\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"LOCK_TYPE\":\"维修锁机\",\n" +
//                "                    \"PROCESS\":\"TS00001\",\n" +
//                "                    \"LOCK_ID\":121\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"LOCK_TYPE\":\"暂停锁机\",\n" +
//                "                    \"PROCESS\":\"TS00001\",\n" +
//                "                    \"LOCK_ID\":120\n" +
//                "                }\n" +
//                "            ]\n" +
//                "        },\n" +
//                "        \"Return\":{\n" +
//                "            \"RETURNCODE\":\"0\",\n" +
//                "            \"RETURNMESSAGE\":\"\"\n" +
//                "        }\n" +
//                "    }\n" +
//                "}";
//
//        BaseEntity<UnlockEntity> bean = (new Gson()).fromJson(ss, new TypeToken<BaseEntity<UnlockEntity>>() {
//        }.getType());
//
//        List<UnlockEntity.LockTypeEntity> lockTypeList = bean.getData().getBody().getLOCK_TYPE_LIST();
//        mLockTypeList.clear();
//        if (lockTypeList.size() > 0){
//            for (UnlockEntity.LockTypeEntity entity : lockTypeList){
//                mLockTypeList.add(entity.getLOCK_TYPE());
//            }
//        }else {
//            showToast("当前设备无需锁机",false);
//            LockmachineActivity.this.finish();
//        }
        //获取锁机类型
        FreeUI_EntityCallBack<BaseEntity<UnlockEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<UnlockEntity>>
                (new TypeToken<BaseEntity<UnlockEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<UnlockEntity>> response) {
                if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                    List<UnlockEntity.LockTypeEntity> lockTypeList = response.body().getData().getBody().getLOCK_TYPE_LIST();
                    mLockTypeList.clear();
                    if (lockTypeList.size() > 0){
                        for (UnlockEntity.LockTypeEntity entity : lockTypeList){
                            mLockTypeList.add(entity.getLOCK_TYPE());
                        }
                    }else {
                        showToast("当前设备无需锁机",false);
                        LockmachineActivity.this.finish();
                    }
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<UnlockEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDALockInfo");
            }
        };
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("EQP_ID", App.EQPID);
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDALockInfo", jsonObject);


    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("锁机");
        freeUI_titleView.setRightListener("提交", v -> {
            String lockType = tvLockType.getText().toString().trim();
            if (lockType.equals("")) {
                showToast("请选择锁机类型", false);
            } else {
                FreeApi_DialogUtils.getConfirmAndCancelDialog(App.app, "您确定要锁机吗？", () -> submit(lockType), () -> {
                }).show(getSupportFragmentManager(), "Lock_DIALOG");
            }
        });
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    private void submit(String lockType) {
//        showToast("锁机成功",true);
//        initData();
//        tvLockType.setText("");
        FreeUI_EntityCallBack<BaseEntity<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<JSONObject>>
                (new TypeToken<BaseEntity<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<JSONObject>> response) {
                if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
//                    showToast("锁机成功",true);

                    showConfirmDialog(LockmachineActivity.this,"锁机成功");
                    tvLockType.setText("");
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(), "PDALock");
            }
        };

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("EQP_ID", App.EQPID);
        jsonObject.addProperty("LOCK_TYPE", lockType);
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDALock", jsonObject);
    }

    @OnClick(R.id.flLockTypeList)
    public void onViewClicked() {
        if (mLockTypeList.size() <= 0) {
            showToast("未获取到锁机类型", false);
        }

        if (lockTypePop != null) {
            if (lockTypePop.isShowing()) {
                lockTypePop.dismiss();
            }
        } else {
            lockTypePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flLockTypeList.getWidth(), mLockTypeList, ivLockTypeListArrow, (code, i) -> {
                tvLockType.setText(code);
            });
        }
        FreeApi_PopUtils.setArrowShow(ivLockTypeListArrow);
        lockTypePop.showAsDropDown(flLockTypeList, 0, 0);

    }

    @Override
    protected void EQPReplaceSuccess(int tag, String content) {

    }
}
